package com.hanlink.dsp.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.hanlink.dsp.util.Constants;
import com.qztech.sdk.R;
import com.qztech.sdk.utils.BLog;
import com.qztech.utils.YzDebug;

import static com.hanlink.dsp.util.Constants.DIENABLED_ALPHA;


public class XOverPassView extends LinearLayout {

    /*    private RadioGroup rdgSlope;

        private RadioButton mRdgButter;
        private RadioButton mRdgLink;*/
    //private CheckBox cbxSlopeSwitch;
    private Button btnFrequencySub;
    private XoverEditText etFrequency;
    private Button btnFrequencyAdd;
    private SeekBar mFreqSeekBar;
    private View mRootView;

    private TextView mTxtHz;

    private int[] mXoverScope;


    private int checkId = 0;

    private int frenquency = 0;

    private boolean high = true;

    private boolean mRefresh = true;

    private boolean isEnabled = true;
    private long mLastChangeTs;

    private OnSlowCheckedChangeListener mOnSlowCheckedChangeListener;

    public XOverPassView(Context context) {
        super(context);
        initView();
    }

    public XOverPassView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public XOverPassView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void setValue(int db, int frenquency, boolean enabled) {
        System.out.println("I am set mValue" + frenquency);
        BLog.i("xovere",frenquency + "," + enabled);
        setEnabled(enabled);
        etFrequency.setText(String.valueOf(frenquency));
        this.frenquency = frenquency;
        mFreqSeekBar.setProgress(frenquency - mXoverScope[0]);
    }


    public int getFrenquency() {
        return frenquency;
    }

    public void setXoverScope(int[] scope) {
        mXoverScope = scope;
        mFreqSeekBar.setMax(Math.abs(mXoverScope[1]) - Math.abs(mXoverScope[0]));
        mFreqSeekBar.setProgress(frenquency - mXoverScope[0]);
        String val = String.valueOf(mFreqSeekBar.getProgress() + mXoverScope[0]);
        etFrequency.setText(val);
    }

    //public boolean getChecked(){}
    protected void initView() {
        mXoverScope = getResources().getIntArray(R.array.frequency_scope);
        LayoutInflater.from(getContext()).inflate(com.hanlink.res.R.layout.view_xover_pass, this, true);

        mRootView = findViewById(com.hanlink.res.R.id.rootView);
        mTxtHz = findViewById(com.hanlink.res.R.id.txtHz);
        btnFrequencySub = findViewById(com.hanlink.res.R.id.btn_frequency_sub);
        etFrequency = findViewById(com.hanlink.res.R.id.et_frequency);
        btnFrequencyAdd = findViewById(com.hanlink.res.R.id.btn_frequency_add);
        mFreqSeekBar = findViewById(com.hanlink.res.R.id.seekBar);

        mFreqSeekBar.setMax(Math.abs(mXoverScope[1]) - Math.abs(mXoverScope[0]));

        btnFrequencyAdd.setOnClickListener(mOnClickListener);
        btnFrequencySub.setOnClickListener(mOnClickListener);
        mFreqSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    String val = String.valueOf(seekBar.getProgress() + mXoverScope[0]);
                    if (!val.equals(etFrequency.getText().toString())) {
                        etFrequency.setText(val);
                        frenquency = progress + mXoverScope[0];
                        if (System.currentTimeMillis() - mLastChangeTs > Constants.SEEK_BAR_DELAY) {
                            mLastChangeTs = System.currentTimeMillis();
                            if (mOnSlowCheckedChangeListener != null && mRefresh) {
                                mOnSlowCheckedChangeListener.onCheckedChanged(XOverPassView.this,  frenquency, true);
                            }
                        } else {
                            if (mOnSlowCheckedChangeListener != null && mRefresh) {
                                mOnSlowCheckedChangeListener.onCheckedChanged(XOverPassView.this,  frenquency, false);
                            }
                        }
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                String val = String.valueOf(seekBar.getProgress() + mXoverScope[0]);
                if (!val.equals(etFrequency.getText().toString())) {
                    etFrequency.setText(val);
                }
                if (mOnSlowCheckedChangeListener != null && mRefresh) {
                    mOnSlowCheckedChangeListener.onCheckedChanged(XOverPassView.this,  frenquency, true);
                }
            }
        });

        etFrequency.setValidEditTextListener(() -> validEditTextValue());


    }

    public void validEditTextValue() {
        Editable editable = etFrequency.getText();
        try {
            if (editable == null || "".equals(editable.toString().trim())) {
                frenquency = 20;
                etFrequency.setText("20");
            } else {
                frenquency = Integer.parseInt(editable.toString());
                if (frenquency < mXoverScope[0]) {
                    frenquency = mXoverScope[0];
                    etFrequency.setText(frenquency + "");
                } else if (frenquency > mXoverScope[1]) {
                    frenquency = mXoverScope[1];
                    etFrequency.setText(frenquency + "");
                }
            }
            mFreqSeekBar.setProgress(frenquency - mXoverScope[0]);
            if (mOnSlowCheckedChangeListener != null && mRefresh)
                mOnSlowCheckedChangeListener.onCheckedChanged(XOverPassView.this,  frenquency, true);
            mRefresh = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public void setHzText(int hzText) {
        mTxtHz.setText(hzText);
    }

    final OnClickListener mOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == com.hanlink.res.R.id.btn_frequency_add) {
                judgeValue(etFrequency, 1);
            } else if (id == com.hanlink.res.R.id.btn_frequency_sub) {
                judgeValue(etFrequency, -1);
            }
        }
    };

    private void judgeValue(TextView view, int value) {
        if (!TextUtils.isEmpty(view.getText())) {
            int oldValue = Integer.parseInt(String.valueOf(view.getText()));
            oldValue = oldValue + value;
            if (oldValue > mXoverScope[1]) {
                oldValue = mXoverScope[1];
            } else if (oldValue < mXoverScope[0]) {
                oldValue = mXoverScope[0];
            }
            view.setText(String.valueOf(oldValue));
        } else {
            view.setText(String.valueOf(mXoverScope[0]));
        }
        try {
            frenquency = Integer.parseInt(view.getText().toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        if (mOnSlowCheckedChangeListener != null && mRefresh)
            mOnSlowCheckedChangeListener.onCheckedChanged(XOverPassView.this,  frenquency, true);
    }



    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        isEnabled = enabled;
        mTxtHz.setEnabled(enabled);
        btnFrequencySub.setEnabled(enabled);
        mFreqSeekBar.setAlpha(enabled?1f:DIENABLED_ALPHA);
        etFrequency.setEnabled(enabled);
        mFreqSeekBar.setEnabled(enabled);
        btnFrequencyAdd.setEnabled(enabled);

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (mTopView != null){
            setPadding(getPaddingLeft(), mTopView.getHeight()/2, getPaddingRight(), getPaddingRight());
        }
    }

    public boolean isEnable() {
        return isEnabled;
    }

    public void setOnSlowCheckedChangeListener(OnSlowCheckedChangeListener mOnSlowCheckedChangeListener) {
        this.mOnSlowCheckedChangeListener = mOnSlowCheckedChangeListener;
    }

    public boolean isHigh() {
        return high;
    }

    public void setHigh(boolean high) {
        this.high = high;
    }

    public int getCheckId() {
        return checkId;
    }

    public void setCheckId(int checkId) {
        this.checkId = checkId;
    }

    private View mTopView;
    public void setTopLayoutView(RadioButton topView) {
        mTopView = topView;
    }

    private class AutoFlingRunnable implements Runnable {

        int mSeed;

        AutoFlingRunnable(int seed) {
            this.mSeed = seed;
        }

        @Override
        public void run() {
            judgeValue(etFrequency, mSeed);
        }
    }

    public interface OnSlowCheckedChangeListener {

        void onCheckedChanged(XOverPassView view,  int frenquency, boolean sendCmd);
    }


}
